package com.android.yummycookies.service.storer.imagesStorer.binder;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import android.graphics.Bitmap;
import android.os.Binder;
import android.util.SparseArray;

import com.android.yummycookies.BuildConfig;
import com.android.yummycookies.asyncTask.LoadBitmapThread;
import com.android.yummycookies.asyncTask.LoadCookieTypesThread;
import com.android.yummycookies.asyncTask.LoadThemeThread;
import com.android.yummycookies.asyncTask.ScaleBitmapThread;
import com.android.yummycookies.model.commonModel.ObserversPool;
import com.android.yummycookies.model.theme.Theme;
import com.android.yummycookies.service.storer.imagesStorer.BitmapsStoreService;
import com.android.yummycookies.types.CookieType;
import com.android.yummycookies.util.Logger;

/**
 * Binder du service BitmapsStoreService
 * 
 * @author julien
 * 
 */
public class BitmapsStoreBinder extends Binder implements IThemeStoreBinder {

	public static final String TAG = BitmapsStoreBinder.class.getSimpleName();

	/** Reference vers le service */
	private final BitmapsStoreService imagesStorerService;
	/** Pool d'observateurs */
	private final ObserversPool observersPool = new ObserversPool();

	/*****************************************************************
	 * Gestion des threads
	 *****************************************************************/
	/** Ensemble des identifiants des bitmap en cours de chargement */
	private final Set<Integer> loadingBitmaps = new HashSet<Integer>();
	/** Ensemble des identifiants des bitmap en cours de redimensionnement */
	private final Set<Integer> scalingBitmaps = new HashSet<Integer>();

	/**
	 * Constructeur
	 * 
	 * @param imageStorerService
	 *            une reference vers le service
	 */
	public BitmapsStoreBinder(BitmapsStoreService imageStorerService) {
		this.imagesStorerService = imageStorerService;
		Logger.logDebug(TAG, TAG + " created");
	}

	/*************************************************************
	 * Getteur
	 *************************************************************/
	public ObserversPool getObserversPool() {
		return observersPool;
	}

	/*************************************************************
	 * IBitmapLoader
	 *************************************************************/

	@Override
	public Bitmap getBitmap(int id) {
		Logger.logDebug(TAG, "Get bitmap", id);
		return imagesStorerService.getStorer().get(id);
	}

	@Override
	public SparseArray<Bitmap> getAllBitmaps(Set<Integer> ids) {
		Logger.logDebug(TAG, "Get all bitmaps", ids);
		return imagesStorerService.getStorer().getAll(ids);
	}

	@Override
	public void loadBitmap(int id) {
		Logger.logDebug(TAG, "Load bitmap", id);
		LoadBitmapThread lbt = new LoadBitmapThread(
				imagesStorerService.getApplicationContext(),
				imagesStorerService.getStorer(), loadingBitmaps, id);
		lbt.getObservablePool().addAllObservers(observersPool);
		lbt.start();
	}

	@Override
	public void loadAllBitmaps(Set<Integer> ids) {
		Logger.logDebug(TAG, "Load all bitmaps", ids);
		LoadBitmapThread lbt = new LoadBitmapThread(
				imagesStorerService.getApplicationContext(),
				imagesStorerService.getStorer(), loadingBitmaps, ids);
		lbt.getObservablePool().addAllObservers(observersPool);
		lbt.start();
	}

	@Override
	public void removeBitmap(int id) {
		Logger.logDebug(TAG, "Remove bitmap", id);
		imagesStorerService.getStorer().remove(id);
	}

	@Override
	public void removeAllBitmaps(Set<Integer> ids) {
		Logger.logDebug(TAG, "Remove all bitmaps", ids);
		imagesStorerService.getStorer().removeAll(ids);
	}

	/***************************************************************
	 * IScaledBitmapLoader
	 ***************************************************************/

	@Override
	public Bitmap getScaledBitmap(int id) {
		Logger.logDebug(TAG, "Get scaled bitmap", id);
		return imagesStorerService.getStorer().getScaledBitmap(id);
	}

	@Override
	public SparseArray<Bitmap> getAllScaledBitmaps(Set<Integer> ids) {
		Logger.logDebug(TAG, "Get all scaled bitmaps", ids);
		return imagesStorerService.getStorer().getAllScaledBitmaps(ids);
	}

	@Override
	public void scaleBitmap(int id, int height, int width) {
		Logger.logDebug(TAG, "Scale bitmap", id);
		ScaleBitmapThread sbt = new ScaleBitmapThread(
				imagesStorerService.getStorer(), height, width, scalingBitmaps,
				id);
		sbt.getObservablePool().addAllObservers(observersPool);
		sbt.start();
	}

	@Override
	public void scaleAllScaledBitmaps(Set<Integer> ids, int height, int width) {
		Logger.logDebug(TAG, "Scale all scaled bitmaps", ids);
		ScaleBitmapThread sbt = new ScaleBitmapThread(
				imagesStorerService.getStorer(), height, width, scalingBitmaps,
				ids);
		sbt.getObservablePool().addAllObservers(observersPool);
		sbt.start();
	}

	@Override
	public void removeScaleBitmap(int id) {
		Logger.logDebug(TAG, "Remove scaled bitmap", id);
		imagesStorerService.getStorer().removeScaledBitmap(id);
	}

	@Override
	public void removeAllScaledBitmaps(Set<Integer> ids) {
		Logger.logDebug(TAG, "Remove all scaled bitmaps", ids);
		imagesStorerService.getStorer().removeAllScaledBitmaps(ids);
	}

	/*************************************************************
	 * IBitmapLoader
	 *************************************************************/

	@Override
	public void addCookieTypes(int height, int width,
			List<CookieType> cookieTypes) {
		Logger.logDebug(TAG, "Add element types", cookieTypes);
		LoadCookieTypesThread lctt = new LoadCookieTypesThread(
				imagesStorerService.getApplicationContext(),
				imagesStorerService.getStorer(), loadingBitmaps,
				scalingBitmaps, height, width, cookieTypes);
		lctt.getObservablePool().addAllObservers(observersPool);
		lctt.start();
	}

	@Override
	public void removeCookieTypes(List<CookieType> cookieTypes) {
		Logger.logDebug(TAG, "Remove element types", cookieTypes);
		for (CookieType ct : cookieTypes) {
			imagesStorerService.getStorer().removeAll(ct.getBitmapsId());
			imagesStorerService.getStorer().removeAllScaledBitmaps(
					ct.getScalableBitmapsId());
		}
	}

	@Override
	public void addTheme(Theme theme, int width, int height) {
		Logger.logDebug(TAG, "Add theme");
		LoadThemeThread ltt = new LoadThemeThread(imagesStorerService.getApplicationContext(),
				imagesStorerService.getStorer(), width, height, scalingBitmaps,
				theme);
		ltt.getObservablePool().addAllObservers(observersPool);
		ltt.start();
		
	}

	@Override
	public void removeTheme(Theme theme, int width, int height) {
		// TODO Auto-generated method stub
		
	}

}
